package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.CollectionBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/6/28.
 */
   public class CollectionListAdapter extends BaseAdapter{

    private Context context;
    List<CollectionBean.ListBean> list;


    public CollectionListAdapter(Context mContext, List<CollectionBean.ListBean> list) {
        this.context=mContext;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list != null ? list.size():0;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void addView(List<CollectionBean.ListBean> list){
        if(list == null || list.size()<=0){
            return;
        }
        list.addAll(list);
    }
    public void notIfy(){
        notifyDataSetChanged();
    }
    public void clear(){
        list.clear();
    }
    public void setList(List<CollectionBean.ListBean> list){
        this.list=list;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder=null;
        if(view == null){
            view= LayoutInflater.from(context).inflate(R.layout.item_collection,null);
            viewHolder=new ViewHolder(view);
            view.setTag(viewHolder);
            AutoUtils.auto(view);
        }else{
            viewHolder= (ViewHolder) view.getTag();
        }
        final  CollectionBean.ListBean listBean = list.get(i);
        String []nUrl=listBean.getHeadImg().split("\\.");
        StringBuffer buffer=new StringBuffer();
        if(nUrl[1].equals("jpg")){
            buffer.append(nUrl[0]).append("_500.jpg");
        }else if(nUrl[1].equals("png")){
            buffer.append(nUrl[0]).append("_500.png");
        }else if(nUrl[1].equals("jpeg")){
            buffer.append(nUrl[0]).append("_500.jpeg");
        }
//        buffer.append(aa[0]).append("_360.jpg");
        Glide.with(context).load(OKUtilAPI.imgBaseUrl+buffer).into(viewHolder.iv_ping);
        viewHolder.content.setText(listBean.getName());
        viewHolder.tv_yuan.setText("￥"+ NetUtils.setDecimal(listBean.getPrice()));
        viewHolder.iv_de.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                httpDet(listBean);
            }
        });
        return view;
    }

    private void httpDet(final CollectionBean.ListBean listBean) {
        OkUtilCallback callback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                try{
                    JSONObject jsonOb=new JSONObject(response);
                    if("OK".equals(jsonOb.getString("result"))){
                        list.remove(listBean);
                        notIfy();
                        BaseApplication.showToast("删除成功");
                    };

                }catch (Exception e){
                    BaseApplication.showToast("删除失败");
                }

            }
            @Override
            public void onError(Call call, Exception e, int id) {
                BaseApplication.showToast("删除失败");
            }
        };
        String userId = SPUtils.getStringData(context, SPUtils.USERID);
        OKUtilAPI.post(OKUtilAPI.QUXIAO, "userId=" + userId+"&ids="+listBean.getId()+"&type="+0,callback);
    }

    public static class ViewHolder
    {
        @BindView(R.id.iv_ping)
        ImageView iv_ping;
        @BindView(R.id.content)
        TextView content;
        @BindView(R.id.tv_yuan)
        TextView tv_yuan;
        @BindView(R.id.tv_te)
        TextView tv_te;
        @BindView(R.id.iv_de)
        ImageView iv_de;

        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
